package com.semidata.trp.decision.utils;

import com.semidata.rtd.core.json.JsonArray;
import com.semidata.rtd.core.json.JsonObject;
import com.semidata.trp.message.business.Business;
import com.semidata.trp.message.business.Business.BudgetOrganization;
import com.semidata.trp.message.business.Business.BudgetStrategy;

public class ProtoUtils {

	public static byte[] businessDataJsonToProto(JsonObject obj) {

		Business.BusinessData.Builder builder = Business.BusinessData
				.newBuilder();

		JsonArray positionArray = obj.getArray("positions");

		for (int i = 0; i < positionArray.size(); i++) {
			JsonObject positionObj = positionArray.get(i);
			Business.Position.Builder positionBuilder = Business.Position
					.newBuilder();
			positionBuilder.setId(positionObj.getInteger("id"));
			positionBuilder.setWidth(positionObj.getInteger("width"));
			positionBuilder.setHeight(positionObj.getInteger("height"));
			positionBuilder.setSize(positionObj.getInteger("size"));
			positionBuilder.setMin(positionObj.getInteger("min"));
			positionBuilder.setMax(positionObj.getInteger("max"));
			JsonArray typeArray = positionObj.getArray("types");
			for (int j = 0; j < typeArray.size(); j++) {
				positionBuilder.addType((String) typeArray.get(j));
			}
			builder.addPositions(positionBuilder.build());
		}

		JsonArray resourceArray = obj.getArray("resources");
		for (int i = 0; i < resourceArray.size(); i++) {
			JsonObject resourceObj = resourceArray.get(i);
			Business.Resource.Builder resourceBuilder = Business.Resource
					.newBuilder();
			resourceBuilder.setId(resourceObj.getInteger("id"));
			resourceBuilder.setPositionId(resourceObj.getInteger("positionId"));
			builder.addResources(resourceBuilder.build());
		}

		JsonArray materialArray = obj.getArray("materials");
		for (int i = 0; i < materialArray.size(); i++) {
			JsonObject materialObj = materialArray.get(i);
			Business.Material.Builder materialBuilder = Business.Material
					.newBuilder();
			materialBuilder.setId(materialObj.getInteger("id"));
			materialBuilder.setWidth(materialObj.getInteger("width"));
			materialBuilder.setHeight(materialObj.getInteger("height"));
			materialBuilder.setSize(materialObj.getInteger("size"));
			materialBuilder.setType(materialObj.getString("type"));
			materialBuilder.setUrl(materialObj.getString("url"));
			materialBuilder
					.setLandingPage(materialObj.getString("landingPage"));
			builder.addMaterials(materialBuilder.build());
		}

		JsonArray strategyArray = obj.getArray("strategies");
		for (int i = 0; i < strategyArray.size(); i++) {
			JsonObject strategyObj = strategyArray.get(i);
			Business.Strategy.Builder strategyBuilder = Business.Strategy
					.newBuilder();
			strategyBuilder.setId(strategyObj.getInteger("id"));
			strategyBuilder.setOrgId(strategyObj.getInteger("orgId"));
			strategyBuilder.setPriority(strategyObj.getInteger("priority"));
			strategyBuilder.setFrequency(strategyObj.getInteger("frequency"));
			strategyBuilder
					.setLandingPage(strategyObj.getString("landingPage"));

			JsonArray resourcePriceArray = strategyObj
					.getArray("resourcePrices");
			for (int j = 0; j < resourcePriceArray.size(); j++) {
				JsonObject resourcePriceObj = resourcePriceArray.get(j);
				Business.Strategy.ResourcePrice.Builder resourcePriceBuilder = Business.Strategy.ResourcePrice
						.newBuilder();
				resourcePriceBuilder.setResourceId(resourcePriceObj
						.getInteger("resourceId"));
				resourcePriceBuilder.setPrice(resourcePriceObj.getNumber(
						"price").doubleValue());
				strategyBuilder.addResourcePrices(resourcePriceBuilder.build());
			}

			JsonArray materialWeightArray = strategyObj
					.getArray("materialWeights");
			for (int j = 0; j < materialWeightArray.size(); j++) {
				JsonObject materialWeightObj = materialWeightArray.get(j);
				Business.Strategy.MaterialWeight.Builder materialWeightBuilder = Business.Strategy.MaterialWeight
						.newBuilder();
				materialWeightBuilder.setMaterialId(materialWeightObj
						.getInteger("materialId"));
				materialWeightBuilder.setWeight(materialWeightObj
						.getInteger("weight"));
				strategyBuilder.addMaterialWeights(materialWeightBuilder
						.build());
			}

			JsonArray areaArray = strategyObj.getArray("areas");
			for (int j = 0; j < areaArray.size(); j++) {
				String area = areaArray.get(j);
				strategyBuilder.addAreas(area);
			}

			JsonArray hourArray = strategyObj.getArray("hours");
			for (int j = 0; j < hourArray.size(); j++) {
				Integer hour = hourArray.get(j);
				strategyBuilder.addHours(hour);
			}
			builder.addStrategies(strategyBuilder.build());
		}

		return builder.build().toByteArray();
	}

	public static byte[] budgetDataJsonToProto(JsonObject obj) {
		Business.BudgetData.Builder builder = Business.BudgetData.newBuilder();

		JsonArray orgArray = obj.getArray("orgs");
		for (int i = 0; i < orgArray.size(); i++) {
			BudgetOrganization.Builder orgBuilder = Business.BudgetOrganization
					.newBuilder();
			JsonObject jb = orgArray.get(i);
			orgBuilder.setId(jb.getInteger("id"));
			orgBuilder.setIn(jb.getInteger("in"));
			orgBuilder.setOut(jb.getInteger("out"));
			orgBuilder.setAlly(jb.getInteger("ally"));
			builder.addOrganizations((int) orgArray.get(i), orgBuilder);
		}

		JsonArray strategyArray = obj.getArray("strategies");
		for (int i = 0; i < strategyArray.size(); i++) {
			BudgetStrategy.Builder straBuilder = Business.BudgetStrategy
					.newBuilder();
			JsonObject jb = orgArray.get(i);
			straBuilder.setId(jb.getInteger("id"));
			straBuilder.setIn(jb.getInteger("in"));
			straBuilder.setOut(jb.getInteger("out"));
			straBuilder.setAlly(jb.getInteger("ally"));
			builder.addStrategies((int) orgArray.get(i), straBuilder);
			//builder.addStrategies((int) strategyArray.get(i));
		}
		return builder.build().toByteArray();
	}

}
